var _grid = $('#jgTable');
gridWidth = $('.u_toolbar');

var _postData = {
    Method: 'Q',
    Content:{
        ShowPutaway: 'N',
        SearchText: ''
    }
};
for(i in postData){
    _postData[i] = postData[i];
}

$(function(){

    _gridInit();

    $('li[data-opt="add"]').on('click', addCode);
    $("li[data-opt='edit']").on('click', function () {
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        editCode(rowId);    
    });
    $("li[data-opt='delete']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        deleteCode(rowId);
    });
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });
    $("li[data-opt='check']").on('click', check);

});

function picFormatter(cellvalue, options, rowdata){
    if(rowdata.Piclocation){
        return '<img src="'+PicAddress+rowdata.Piclocation+'"  style="width:100px">';
    }else{
        return '暂无图片';
    }
}
function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_sort" title="物料排序"><i class="fa fa-sort-alpha-asc"></i></a>'+
            '<a href="javascript:0" opt-data="'+grid[0].id+'_edit" title="编辑"><i class="fa fa-pencil"></i></a>'+
            '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.url = mdmPath + '/MDM0009/MDM000902';
    _gridData.colModel = [
        {label:'代码', name:'Code', width:60},
        {label:'品牌', name:'Brand', width:60},
        {label:'名称', name:'Name'},
        {label:'图片', name:'Piclocation', width:100, formatter:picFormatter, sortable:false},
        {label:'上架产品数', name:'U_Count', width:60, sortable:false},
        {label:'操作', name:'Options', width:60, align:'center', formatter: alarmFormatter, sortable:false}
    ];
    _gridData.sortname = 'Code';
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        $('a[opt-data="jgTable_sort"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            sortCode(rowId);
        });
        $('a[opt-data="jgTable_edit"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            editCode(rowId);
        });
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            deleteCode(rowId);
        });
    };
    _gridData.ondblClickRow = function(rowid,iRow,iCol,e){
        editCode(rowid);
    };
    gridInit(_gridData);
    gridSearch = _postData;
}

//Options
function addCode(){
    window.open('MDM000902.html', '_blank');  
}
function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('MDM000902.html?Code=' + rowData.Code, '_blank');
    }else{
        myAlert('请选择您要编辑的数据');
    }   
}
function sortCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('MDM000903.html?Code=' + rowData.Code, '_blank');
    }else{
        myAlert('请选择您要编辑的数据');
    }  
};
function deleteCode(rowId){
    if(rowId){  //判断是否选中有效行

    }else{
        myAlert('请选择您要删除的数据');
    }   
}
function check(){
    //console.log('dianji')
    if($(this).find('i').hasClass('fa-square-o')){
        $(this).find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
        _postData.Content = {
            ShowPutaway: 'Y',
            SearchText: ''
        };
        _grid.trigger("reloadGrid");
    }else{
        $(this).find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
        _postData.Content = {
            ShowPutaway: 'N',
            SearchText: ''
        };
        _grid.trigger("reloadGrid");
    };
}
